<?php
/**
 * Created by PhpStorm.
 * User: woody
 * Date: 17-2-16
 * Time: 下午10:46
 */

namespace App\Http\Controllers\Admin;

use App\Models\Viptwolist;
use App\Models\Vipuser;
use App\Models\PersonInfo;
use Illuminate\Http\Request;

class VipuserController extends AdminController {

    public function __construct()
    {
        parent::__construct();
        $this->_response['_active']['_model']       = 'vipuser';
        $this->_response['_active']['_action']      = '';
        $this->_response['_title']                  = '小一农货-vip用户管理';
    }
    public function index(Request $request){
        $page = $request->get('page', 1);
        $search = $request->all();
        $this->_response['_active']['_action']      = 'list';

        $vipuserModel = new Vipuser();
        $data = $vipuserModel->getData($search, $page, 20);
        $this->_response['vipuser'] = $data;
        $this->_response['vipuser']['pageData']['page'] = $page;
        $this->_response['vipuser']['pageData']['pageHtml'] = self::getPageHtml($page , $this->_response['vipuser']['pageData']['lastPage'] , '/admin/vipuser?');
      //  return  $this->_response;
        return view('admin.vipuser.list', $this->_response);
    }

    public function viptwolist(Request $request){
        $page=$request->get('page',1);
        $search=$request->all();
        $this->_response['_active']['_action']='twolist';

        $viptwolistModel = new Viptwolist();
        $data = $viptwolistModel->getData($search, $page, 20);
        $this->_response['vipuser'] = $data;
        $this->_response['vipuser']['pageData']['page'] = $page;
        $this->_response['vipuser']['pageData']['pageHtml'] = self::getPageHtml($page , $this->_response['vipuser']['pageData']['lastPage'] , '/admin/vipuser?');
        //return  $this->_response;
        return view('admin.vipuser.twolist', $this->_response);

    }

    public function add(){
        $this->_response['_active']['_action']      = 'add';
        return view('admin.vipuser.add', $this->_response);
    }

    public function doedit(\App\Http\Requests\Vipuser $request){
        $data=$request->all();
        $id=$data['id'];
        $saveData =[
            'user_level'=>'普通会员',
             'user_id'=>$id,
        ];
        $setData =[
            'ispartner'=>1,
            'updated_at' => date('Y-m-d H:i:s', time())
        ];
       //更改用户表的等级
        $userData =[
            'user_level'=>1,
            'updated_at' => date('Y-m-d H:i:s', time())
        ];
        $user=new PersonInfo();
        $user->setData($id,$userData);
        $vipuserModel = new Vipuser();
        //将vip修改为合伙人的信息加入合伙人表
        $vipuserModel->savepartner($saveData);
        if($vipuserModel->setData($id,$setData)){
            return redirect('admin/vipuser');
        }else{
            return response()->json(['code' => -1, 'msg' => '修改失败']);
        }
    }

    public function delete($id){
        $vipuserModel = new Vipuser();
        $data=[
            'deleted_at'=>date('Y-m-d H:i:s',time())
        ];
        if($vipuserModel->delData($id,$data)){
            return response()->json(['code' => 0, 'msg' => '删除成功']);
           // return redirect('/admin/vipuser');
        }else{
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }


    public function twoedit($id){
        $this->_response['vipuser'] = Viptwolist::where('id', $id)->first();

        return view('admin.vipuser.twoedit', $this->_response);
    }

    public function twodoedit(\App\Http\Requests\Vipuser $request){

      //  return "ddd";
        $data = $request->all();
        $saveData =[
            'avatar' => $data['img'],
            'user_name' => $data['user_name'],

            'updated_at' => date('Y-m-d H:i:s', time())
        ];

        $vipuserModel = new Viptwolist();
        if($vipuserModel->setData($data['id'],$saveData)){
            return response()->json(['code' => 0, 'msg' =>'修改成功']);

        }else{
            return response()->json(['code' => -1, 'msg' => '修改失败']);
        }
    }

    public function twodelete($id){
        $vipuserModel = new Viptwolist();
        $data=[
            'deleted_at'=>date('Y-m-d H:i:s',time())
        ];
        if($vipuserModel->delData($id,$data)){
            return response()->json(['code' => 0, 'msg' => '删除成功']);
            // return redirect('/admin/vipuser');
        }else{
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }




}